home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / libs / mufs_usergroup.lha / usergroup / time.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  206 lines

  1. RCS_ID_C="$Id: time.c,v 2.1 1994/02/17 02:21:58 ppessi Exp $";
  2. /*
  3.  * time.c --- GMT time functions
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * This file is part of the AmiTCP/IP User Library.
  8.  *
  9.  * Copyright ⌐ 1993 AmiTCP/IP Group, <AmiTCP-Group@hut.fi>
  10.  *                  Helsinki University of Technology, Finland.
  11.  *
  12.  * Created      : Wed Sep 15 01:25:26 1993 ppessi
  13.  * Last modified: Thu Jan 27 12:12:57 1994 ppessi
  14.  */
  15.  
  16. #include <sys/errno.h>
  17. #include <assert.h>
  18. #include <sys/time.h>
  19. #include <devices/timer.h>
  20.  
  21. #include "base.h"
  22. #include "libfunc.h"
  23.  
  24. /*
  25.  * Time zone support for the gettimeofday. Zeroes default to the GMT
  26.  * without daylight saving.
  27.  */
  28. static struct timezone __time_zone = {0,0};
  29.  
  30. /*
  31.  * Seconds to to the system time (seconds from 00:00 1.1.1978)
  32.  * to the GMT (seconds from 00:00 1.1.1970).
  33.  * _STIopenTimer() adds the local time seconds west from GMT to this
  34.  * value, so the local time gets converted to the GMT.
  35.  */
  36. #define AMIGA_OFFSET ((8L*365 + 8/4)*24*60*60)
  37. static long __local_to_GMT = AMIGA_OFFSET;
  38.  
  39. static struct timerequest timereq[1] = { 0 };
  40. struct Library *TimerBase;
  41.  
  42. /* This structure must only be allocated by locale.library and is READ-ONLY! */
  43. struct Locale
  44. {
  45.     STRPTR    loc_LocaleName;      /* locale's name         */
  46.     STRPTR    loc_LanguageName;      /* language of this locale     */
  47.     STRPTR    loc_PrefLanguages[10];      /* preferred languages     */
  48.     ULONG    loc_Flags;          /* always 0 for now         */
  49.  
  50.     ULONG    loc_CodeSet;          /* always 0 for now         */
  51.     ULONG    loc_CountryCode;      /* user's country code     */
  52.     ULONG    loc_TelephoneCode;      /* country's telephone code     */
  53.     LONG    loc_GMTOffset;          /* minutes from GMT         */
  54.  
  55. /* deleted the rest to save space */
  56. };
  57.  
  58. void CloseLocale( struct Locale *locale );
  59. struct Locale *OpenLocale( STRPTR name );
  60.  
  61. #pragma libcall LocaleBase CloseLocale 2A 801
  62. #pragma libcall LocaleBase OpenLocale 9C 801
  63.  
  64. int TimeInit(void)
  65. {
  66.   if (OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)timereq, 0)) {
  67.     TimerBase = NULL;
  68.     return -1;
  69.   } else {
  70.     TimerBase = (struct Library*)timereq->tr_node.io_Device;
  71.  
  72.     if (TimerBase->lib_Version >= 36) {
  73.       /*
  74.        * Initialize time zone information for the gettimeofday()
  75.        * First try to open locale (2.1 and up), and if that fails,
  76.        * try to read environment variable TZ.
  77.        */
  78.       void *LocaleBase;
  79.       struct Locale *thisLocale = NULL;
  80.  
  81.       if ((LocaleBase = OpenLibrary("locale.library", 38)) != NULL) {
  82.     if ((thisLocale = OpenLocale(NULL)) != NULL) {
  83.       /*
  84.        * Update time zone minutes west from GMT.
  85.        */
  86.       __time_zone.tz_minuteswest = thisLocale->loc_GMTOffset;
  87.       CloseLocale(thisLocale);
  88.     }
  89.     CloseLibrary(LocaleBase);
  90.       }
  91.       if (!thisLocale) { /* if locale information was not available */
  92.     short len;
  93.     long value;
  94.     char zone[10];
  95.  
  96.     BPTR file = Open("ENV:TZ", MODE_OLDFILE);
  97.     if (file) {
  98.       len = Read(file, zone, sizeof(zone));
  99.       if (len > 3) {
  100.         zone[len] = '\000';
  101.         /* should interpret floats as well! */
  102.         if (StrToLong(zone+3, &value) > 0) {
  103.           /*
  104.            * Update time zone minutes west from GMT.
  105.            */
  106.           __time_zone.tz_minuteswest = (short)value * (short)60;
  107.         }
  108.       }
  109.       Close(file);
  110.     }
  111.       }
  112.  
  113.       /*
  114.        * Update local time seconds to GMT translation
  115.        */
  116.       __local_to_GMT += (short)__time_zone.tz_minuteswest * (short)60;
  117.  
  118.       return 0;
  119.     }
  120.  
  121.     return -1;
  122.   }
  123. }
  124.  
  125. void TimeCleanup(void)
  126. {
  127.   if (TimerBase) {
  128.     CloseDevice((struct IORequest *)timereq), TimerBase = NULL;
  129.   }
  130. }
  131.  
  132. /****i* usergroup.library/gettimeofday *************************************
  133.  
  134.     NAME
  135.         gettimeofday - get date and time
  136.  
  137.     SYNOPSIS
  138.         #include <sys/time.h>
  139.  
  140.         success = gettimeofday(tvp, tzp)
  141.           D0                   A0   A1
  142.  
  143.         int gettimeofday(struct timeval *, struct timezone *)
  144.  
  145.     DESCRIPTION
  146.         The system's notion of the current Greenwich time and the current
  147.         time zone is obtained with the gettimeofday() call.  The time is
  148.         expressed in seconds and microseconds since midnight (0 hour),
  149.         January 1, 1970.  The resolution of the system clock is hardware
  150.         dependent, and the time may be updated continuously or in `ticks.'
  151.         If tp or tzp is NULL, the associated time information will not be
  152.         returned or set.
  153.  
  154.         The structures pointed to by tp and tzp are defined in <sys/time.h>
  155.         as:
  156.  
  157.         struct timeval {
  158.                 long    tv_sec;         \* seconds since Jan. 1, 1970 *\
  159.                 long    tv_usec;        \* and microseconds *\
  160.         };
  161.  
  162.         struct timezone {
  163.                 int     tz_minuteswest; \* of Greenwich *\
  164.                 int     tz_dsttime;     \* type of dst correction to apply *\
  165.         };
  166.  
  167.         The timezone structure indicates the local time zone (measured in
  168.         minutes of time westward from Greenwich), and a flag that, if
  169.         nonzero, indicates that Daylight Saving time applies locally during
  170.         the appropriate part of the year.
  171.  
  172.     RETURN
  173.         A 0 return value indicates that the call succeeded.  A -1 return
  174.         value indicates an error occurred, and in this case an error code is
  175.         available by ug_GetErr() call and it may be stored into the global
  176.         variable errno.
  177.  
  178.     ERRORS
  179.         The following error codes may be available by ug_GetErr() or stored
  180.         in errno:
  181.  
  182.         [EFAULT]  An argument address referenced invalid memory.
  183.  
  184. ****************************************************************************
  185. */
  186.  
  187. SAVEDS ASM int R_gettimeofday(REG(a0) struct timeval *tvp,
  188.                   REG(a1) struct timezone *tzp)
  189. {
  190.   if (tvp) {
  191.     GetSysTime(tvp);
  192.     tvp->tv_sec += __local_to_GMT;
  193.   }
  194.   if (tzp)
  195.     *tzp = __time_zone;
  196.  
  197.   return 0;
  198. }
  199.  
  200. SAVEDS ASM int R_settimeofday(REG(a0) struct timeval *tvp,
  201.                   REG(a1) struct timezone *tzp)
  202. {
  203.   return -1;
  204. }
  205.  
  206.